package com.qmx.launcher.activity;


import android.content.Context;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.open.androidtvwidget.bridge.RecyclerViewBridge;
import com.open.androidtvwidget.leanback.adapter.GeneralAdapter;
import com.open.androidtvwidget.leanback.recycle.LinearLayoutManagerTV;
import com.open.androidtvwidget.leanback.recycle.RecyclerViewTV;
import com.open.androidtvwidget.view.MainUpView;
import com.qmx.launcher.QMXApplication;
import com.qmx.launcher.R;
import com.qmx.launcher.adapter.ContactRecyclerViewPresenter;
import com.qmx.launcher.bean.ContactModel;
import com.qmx.launcher.utils.RealmUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmList;

//import com.qmx.qmxapp.activity.CreateActivity;

/**
 * @author ChengXiangtong
 * @create 2018/10/29
 * @Describe
 */
public class MeetingFragment extends Fragment implements RecyclerViewTV.OnItemListener {
    private Context mContext;
    private RecyclerViewTV mRecyclerView;
    private EditText contact_group_tips;
    private MainUpView mainUpView1;
    private RecyclerViewBridge mRecyclerViewBridge;
    private View oldView;
    private ContactRecyclerViewPresenter mRecyclerViewPresenter;
    private GeneralAdapter mGeneralAdapter;
    private int roomId;
    private int password;

    private Realm defaultRealm;
    private RealmList<ContactModel> itemList;
    private RealmUtils realmUtils;
    private View mView;


    public MeetingFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the fragment_meeting for this fragment
        mView = inflater.inflate(R.layout.fragment_meeting, container, false);

        defaultRealm = QMXApplication.getRealmInstance();
        realmUtils = new RealmUtils(defaultRealm);

        mRecyclerView = (RecyclerViewTV) mView.findViewById(R.id.rv_contact_list);
        contact_group_tips = mView.findViewById(R.id.contact_group_tips);
        mainUpView1 = (MainUpView) mView.findViewById(R.id.mainUpView1);
        mainUpView1.setEffectBridge(new RecyclerViewBridge());
        // 注意这里，需要使用 RecyclerViewBridge 的移动边框 Bridge.
        mRecyclerViewBridge = (RecyclerViewBridge) mainUpView1.getEffectBridge();
        mRecyclerViewBridge.setUpRectResource(R.drawable.blue_arrow);
        float density = getResources().getDisplayMetrics().density;
        RectF receF = new RectF(getDimension(R.dimen.w_55) * density, getDimension(R.dimen.h_5) * density,
                getDimension(R.dimen.w_5) * density, getDimension(R.dimen.h_5) * density);
        mRecyclerViewBridge.setDrawUpRectPadding(receF);

        saveOrUpdate();
        recyclerViewLinerLayout(RecyclerView.VERTICAL);

        mRecyclerView.setOnItemListener(this);
        // item 单击事件处理.
        mRecyclerView.setOnItemClickListener(new RecyclerViewTV.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerViewTV parent, View itemView, int position) {
                Toast.makeText(mContext, "点击位置" + position, Toast.LENGTH_SHORT).show();
            }
        });
        return mView;

    }

    public void saveOrUpdate() {
        //        RealmResults<ContactModel> list = defaultRealm.where(ContactModel.class)
        //                .findAll();
        final List<ContactModel> list = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            ContactModel contactModel = new ContactModel();
            contactModel.setName("修改 " + i);
            contactModel.setMobile(123456789 + i);
            contactModel.setMdate(new Date());
            contactModel.setId(i);
            list.add(contactModel);
        }
        realmUtils.insertOrUpdateBatch(list);

        defaultRealm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //                realmUtils.insertOrUpdateBatch(list);
                //                    realm.copyToRealmOrUpdate(contactModel);
                //                                                         parentContact.getItemList().add(contactModel);
                //                realm.copyToRealmOrUpdate(list);
            }
        });


        //        RealmResults<ContactModel> all = defaultRealm.where(ContactModel.class)
        //                .findAll();
        //        //                mRealm.beginTransaction();
        //        //                for (int i = 0; i < all.size(); i++) {
        //        //                    all.get(i).setName("使用查询语句后就可以修改");
        //        //                }
        //        //                mRealm.commitTransaction();
        //        //到这里是不行的，因为迭代器修改时会移除，所以导致隔一修改情况，
        //
        //        // 需要一下操作
        //        defaultRealm.beginTransaction();
        //        OrderedRealmCollectionSnapshot<ContactModel> menSnapshot = all.createSnapshot();
        //        for (int i = 0; i < menSnapshot.size(); i++) {
        //            menSnapshot.get(i).setName("使用查询语句后就可以修改!" + i);
        //        }
        //        defaultRealm.commitTransaction();
    }

    private void recyclerViewLinerLayout(int orientation) {
        LinearLayoutManagerTV layoutManager = new LinearLayoutManagerTV(getContext());
        layoutManager.setOrientation(orientation);
        mRecyclerView.setLayoutManager(layoutManager);
        //        mRecyclerView.setFocusable(false);
        mRecyclerViewPresenter = new ContactRecyclerViewPresenter(defaultRealm.where(ContactModel
                .class).findAll());
        mGeneralAdapter = new GeneralAdapter(mRecyclerViewPresenter);
        mRecyclerView.setAdapter(mGeneralAdapter);
        mRecyclerView.setHasFixedSize(true);
        //        mRecyclerView.setSelectedItemOffset(10, 10); // 测试移动间距.
        mRecyclerView.setFocusable(true);
        mRecyclerView.setDefaultSelect(0);

    }


    public float getDimension(int id) {
        return getResources().getDimension(id);
    }

    @Override
    public void onItemPreSelected(RecyclerViewTV parent, View itemView, int position) {

    }

    @Override
    public void onItemSelected(RecyclerViewTV parent, View itemView, int position) {

    }

    @Override
    public void onReviseFocusFollow(RecyclerViewTV parent, View itemView, int position) {

    }
}
